﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.PhieuThu
{
    public partial class frmXuLy : Form
    {
        public frmXuLy()
        {
            InitializeComponent();
        }
       
        #region Contractor
        string makho = "";
        string tenkho = "";
        public bool kiemtra1 = false;
        bool checkClose = false;
        Entities.PhieuThu phieuthu;
        Entities.PhieuThu temp;
        Entities.PhieuThu ptin;
        DateTime date;
        string hanhdong;
        #endregion

        #region check permission
        public bool CheckPer()
        {
            return true;
        }
        #endregion

        #region Check conflicts

        string maPhieuThu = "";
        // Check Conflic Insert
        public bool CheckConflicInsert()
        {
            try
            {
                maPhieuThu = getID();
                if (phieuthu.MaPhieuThu == maPhieuThu)
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return true;
            }

        }

        //Check Conflic Update
        public bool CheckConflicUpdate()
        {
            try
            {
                Entities.PhieuThu[] mangchitiet = Common.StaticVariable.InterfaceObject.SelectPhieuThu();

                for (int i = 0; i < mangchitiet.Length; i++)
                {
                    if (mangchitiet[i].MaPhieuThu == SbiMaHoaDon.Text.ToString())
                    {
                        return kiemtra(mangchitiet[i]);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        // kiểm tra
        public bool kiemtra(Entities.PhieuThu pc)
        {
            try
            {
                bool kt = true;
                if (temp.DoiTuong != pc.DoiTuong)
                {
                    cbbDoiTuong.Text = pc.DoiTuong;
                    kt = false;
                }
                if (temp.TenDoiTuong != pc.TenDoiTuong)
                {
                    lblTenDoiTuong.Text = pc.TenDoiTuong;
                    kt = false;
                }
                if (temp.DiaChi != pc.DiaChi)
                {
                    lblDiaChiDoiTuong.Text = pc.DiaChi;
                    kt = false;
                }
                if (temp.NguoiNopTien != pc.NguoiNopTien)
                {
                    txtNguoiNopTien.Text = pc.NguoiNopTien;
                    kt = false;
                }
                if (temp.MaDoiTuong != pc.MaDoiTuong)
                {
                    txtMaDoiTuong.Text = pc.MaDoiTuong;
                    kt = false;
                }
                if (temp.CoTaiKhoan != pc.CoTaiKhoan)
                {
                    cbbCoTaiKhoan.Text = pc.CoTaiKhoan;
                    kt = false;
                }
                if (temp.NoTaiKhoan != pc.NoTaiKhoan)
                {
                    lbNoTaiKhoan.Text = pc.NoTaiKhoan;
                    kt = false;
                }
                if (temp.TongTienThanhToan != pc.TongTienThanhToan)
                {
                    txtSoTienThanhToan.Text = pc.TongTienThanhToan;
                    kt = false;
                }
                if (temp.GhiChu != pc.GhiChu)
                {
                    txtGhiChu.Text = pc.GhiChu.ToString();
                    kt = false;
                }
                return kt;
            }
            catch
            {
                return true;

            }

        }

        #endregion

        #region Nghiệp vụ

        // thoát
        public void Thoat()
        {
            try
            {
                checkClose = false;
                this.Close();
            }
            catch
            {
            }
        }

        // thêm
        public void Them()
        {
            try
            {
                if (Entitystatic.ViewFile().MaKho == "NULL" || Entitystatic.ViewFile().MaKho == string.Empty)
                {
                    MessageBox.Show("Bạn hãy cài kho trước khi thêm mới");
                    return;
                }
                if (txtMaDoiTuong.Text == "<F4 - Tra cứu>")
                { return; }

                phieuthu = XuLyObject(Common.Constants.Insert);
                if (CheckConflicInsert())
                {
                    if (Common.StaticVariable.InterfaceObject.InsertUpdatePhieuThu(phieuthu))
                    {
                        checkClose = true;
                        UpdateDuNo(); // update dư nợ vào bảng Chi tiết dư nợ KH or NCC theo đối tượng được chọn
                        this.Close();
                    }
                    else
                    {
                    }
                }
                else
                {
                    MessageBox.Show(Common.MSG.MSG5(phieuthu.MaPhieuThu), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    lbMaPhieuThu.Text = maPhieuThu;
                }
            }
            catch
            {}
        }

        
        public void UpdateDuNo()
        {
            try
            {
                if (cbbDoiTuong.Text == "Khách Hàng")
                {
                   
                    Entities.ChiTietCongNoKH temp = new Entities.ChiTietCongNoKH("1",
                  txtMaDoiTuong.Text
                  , makho, txtSoTienThanhToan.Text, false);
                    Entities.ChiTietCongNoKH[] chitiet = Common.StaticVariable.InterfaceObject.SelectChiTietCongNoKH(temp);
                    if (chitiet != null && chitiet.Length != 0)
                        temp.HanhDong = "ThanhToan"; // Trừ DuNo=DuNo - @DuNo
                    else
                        temp.HanhDong = "Insert";
                    Common.StaticVariable.InterfaceObject.InsertUpdateChiTietCongNoKH(temp);
                }


                if (cbbDoiTuong.Text == "Nhà Cung Cấp")
                {
                  
                    Entities.ChiTietCongNoNCC temp = new Entities.ChiTietCongNoNCC("1",
                   txtMaDoiTuong.Text
                   , makho, txtSoTienThanhToan.Text, false);
                    Entities.ChiTietCongNoNCC[] chitiet = Common.StaticVariable.InterfaceObject.SelectChiTietCongNoNCC(temp);
                    if (chitiet != null && chitiet.Length != 0)
                        temp.HanhDong = "Update"; // Cộng DuNo=DuNo + @DuNo
                    else
                        temp.HanhDong = "Insert";
                    Common.StaticVariable.InterfaceObject.InsertUpdateChiTietCongNoNCC(temp);
             
                }
            }
            catch { }
        }
       

        public void In()
        {
            try
            {
                if (ptin == null)
                {
                    MessageBox.Show("Bạn phải Ghi lại trước khi In", Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                this.Enabled = false;
                BaoCao.frmBaoCaorpt frm = new BaoCao.frmBaoCaorpt(ptin);
                if (frm.kiemtra)
                {
                   frm.kiemtra = false;
                }
                this.Enabled = true;
            }
            catch
            {
            }
        }

        #endregion

        #region Sự kiện của windown
        //Load
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            try
            {
                this.WindowState = FormWindowState.Normal;
                Entities.CaiDatKho kho = Entitystatic.ViewFile();
                makho = kho.MaKho;
                tenkho = kho.TenKho;

                btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnIn.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnGhiLai.MouseDown += new MouseEventHandler(MouseDown_Chung);

                txtSoTienThanhToan.MouseDown += new MouseEventHandler(txtsotienthanhtoan_MouseDown);
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;

            }
            catch
            { }

        }

        // Keys down event
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.F2:
                        {
                            switch (hanhdong)
                            {
                                case Common.Constants.Insert:
                                    {
                                        Them();
                                        break;
                                    }
                            }

                            break;
                        }
                    case Keys.F3:
                        {
                            In();
                            break;
                        }
                    case Keys.Escape:
                        {
                            checkClose = false;
                            this.Close();
                            break;
                        }

                }
            }
            catch
            { }

        }
        //
        private void Window_Closing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!checkClose)
                    if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        e.Cancel = checkClose;
                    }
                    else
                        e.Cancel = !checkClose;
                else
                {
                    if (!kiemtra1)
                            Select(hanhdong);
                    
                }

            }
            catch
            {}
        }

        public delegate void SelectData(string hanhdong);
        public SelectData Select;

        // Select Chage cbbKhoanMuc
        Entities.KMThuChi[] kmtc;
        public void SelectChange_cbbKhoanMuc(object sender, EventArgs e)
        {
            try
            {
                int i = cbbKhoanMuc.SelectedIndex;
                if (i >= 0)
                {
                    for (int j = 0; j < kmtc.Length; j++)
                    {
                        if (cbbKhoanMuc.SelectedValue.ToString() == kmtc[j].MaKhoanMuc)
                        {
                            cbbCoTaiKhoan.Text = kmtc[j].CoTK.ToString();
                            lbNoTaiKhoan.Text = kmtc[j].NoTK.ToString();
                            cbbDoiTuong.Text = kmtc[j].DoiTuong.ToString();
                            txtMaDoiTuong.ReadOnly = false;
                            txtMaDoiTuong.Text = "";
                            lblDiaChiDoiTuong.Text = "";
                            lblMaDoiTuong.Text = "";
                            lblTenDoiTuong.Text = "";
                            return;
                        }

                    }
                }
            }
            catch
            {  }
        }

        private void txtsotienthanhtoan_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                txtSoTienThanhToan.Text = String.Format("{0:0}", txtSoTienThanhToan.Text);
            }
            catch
            { }
        }

        public void LoadInitial()
        {
            try
            {
                int i = cbbKhoanMuc.SelectedIndex;
                if (i >= 0)

                kmtc = Common.StaticVariable.InterfaceObject.SelectKMThuChiString("False");
                {
                    for (int j = 0; j < kmtc.Length; j++)
                    {
                        if (cbbKhoanMuc.SelectedValue.ToString() == kmtc[j].MaKhoanMuc)
                        {
                            cbbCoTaiKhoan.Text = kmtc[j].CoTK.ToString();
                            lbNoTaiKhoan.Text = kmtc[j].NoTK.ToString();
                            cbbDoiTuong.Text = kmtc[j].DoiTuong.ToString();
                            txtMaDoiTuong.ReadOnly = false;
                            txtMaDoiTuong.Text = "";
                            lblDiaChiDoiTuong.Text = "";
                            lblMaDoiTuong.Text = "";
                            lblTenDoiTuong.Text = "";
                            txtMaDoiTuong.Text = "<F4 - Tra cứu>";
                            return;
                        }

                    }
                }
            }
            catch
            {}
        }

        #endregion

        #region  Form xử lý phiếu thu
        // xử lý khi là thêm mới
        public frmXuLy(string hanhdong)
        {
            try
            {
                InitializeComponent();
                this.hanhdong = hanhdong;
                SbiMaHoaDon.Text = getID();
                lbMaPhieuThu.Text = getID();
                ComBoBox_Chung();
                LoadInitial();
                btnIn.Enabled = false;

            }
            catch
            {}
        }

        // xử lý khi là xem
        public frmXuLy(Entities.PhieuThu phieuthu, string hanhdong)
        {
            try
            {
                InitializeComponent();
                this.hanhdong = hanhdong;
                temp = phieuthu;
                ptin = phieuthu;
                SbiMaHoaDon.Text = phieuthu.MaPhieuThu;
                // get data into combov=box
                ComBoBox_Chung();

                // 
                XuLyDuLieu(phieuthu);
                HienThiComBoBoxKhoanMuc(phieuthu.KhoanMuc);
                HienThiComBoBoxCoTaiKhoan(phieuthu.CoTaiKhoan);
                HienThiComBoBoxDoiTuong(phieuthu.DoiTuong);
                HienThiComBoBoxNoTaiKhoan(phieuthu.NoTaiKhoan);

                // 
                panel_HienThiLeft.Enabled = false;
                btnGhiLai.Enabled = false;
            
            }
            catch
            {
            }

        }

        #endregion

        #region Combobox

        public void ComBoBox_Chung()
        {
            try
            {
                GetDataComboboxKhoanMuc();
                GetDataComboboxDoiTuong();
                GetDataComboboxCoTaiKhoan();
                GetDataComboboxNoHienThoi();
                kmtc = Common.StaticVariable.InterfaceObject.SelectKMThuChiString("False");

                cbbKhoanMuc.SelectedValueChanged += new EventHandler(SelectChange_cbbKhoanMuc);

            }
            catch
            {


            }
        }

        // display on combobox with condition khoan mục
        public void HienThiComBoBoxKhoanMuc(string khoanmuc)
        {
            try
            {
                for (int i = 0; i < cbbKhoanMuc.Items.Count; i++)
                {
                    Entities.KMThuChi id = (Entities.KMThuChi)cbbKhoanMuc.Items[i];
                    if (id.TenKhoanMuc == khoanmuc)
                    {
                        cbbKhoanMuc.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch
            { }

        }

        // display on combobox with condition NoTaiKhoan
        public void HienThiComBoBoxNoTaiKhoan(string notaikhoan)
        {
            try
            {
                for (int i = 0; i < lbNoTaiKhoan.Items.Count; i++)
                {
                    Entities.TKKeToan id = (Entities.TKKeToan)lbNoTaiKhoan.Items[i];
                    if (id.MaTKKeToan == notaikhoan)
                    {
                        lbNoTaiKhoan.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch
            {


            }

        }

        // display on combobox with condition Có Taì Khoan
        public void HienThiComBoBoxCoTaiKhoan(string cotaikhoan)
        {
            for (int i = 0; i < cbbCoTaiKhoan.Items.Count; i++)
            {
                Entities.TKKeToan id = (Entities.TKKeToan)cbbCoTaiKhoan.Items[i];
                if (id.MaTKKeToan == cotaikhoan)
                {
                    cbbCoTaiKhoan.SelectedIndex = i;
                    break;
                }
                else
                { continue; }
            }
        }

        // display on combobox with condition Đối tượng
        public void HienThiComBoBoxDoiTuong(string doituong)
        {
            for (int i = 0; i < cbbDoiTuong.Items.Count; i++)
            {
                Entities.GetCombobox id = (Entities.GetCombobox)cbbDoiTuong.Items[i];
                if (id.TenCotTenHienThi == doituong)
                {
                    cbbDoiTuong.SelectedIndex = i;
                    break;
                }
                else
                { continue; }
            }
        }



        // load Khoan muc in table khoan muc thu chi , display by combobox
        private void GetDataComboboxKhoanMuc()
        {
            try
            {
                Entities.KMThuChi[] kmthu = Common.StaticVariable.InterfaceObject.SelectKMThuChiString("False");

                Common.Utilities.XuLyComboBox(cbbKhoanMuc, kmthu, "TenKhoanMuc", "MaKhoanMuc");

            }
            catch { }
        }

        // load Nợ hiện thời in table khoan muc thu chi , display by combobox
        private void GetDataComboboxNoHienThoi()
        {
            Entities.TKKeToan[] tkkt = Common.StaticVariable.InterfaceObject.SelectCoNo("NO");
            Common.Utilities.XuLyComboBox(lbNoTaiKhoan, tkkt, "MaTKKeToan", "MaTKKeToan");
        }

        // load Có tài khoản  in table khoan muc thu chi , display by combobox
        private void GetDataComboboxCoTaiKhoan()
        {
            Entities.TKKeToan[] tkkt = Common.StaticVariable.InterfaceObject.SelectCoNo("CO");

            Common.Utilities.XuLyComboBox(cbbCoTaiKhoan, tkkt, "MaTKKeToan", "MaTKKeToan");
        }

        // load ten doi tuong in table khoan muc thu chi , display by combobox
        private Entities.GetCombobox[] combobox_tendoituong;
        private void GetDataComboboxDoiTuong()
        {
            cbbDoiTuong.Items.Clear();
            Entities.GetCombobox pb = new Entities.GetCombobox();
            pb.TenBang = "KMThuChi";
            pb.TenCotKhoaChinh = "TenKhoanMuc";
            pb.TenCotTenHienThi = "DoiTuong";
            combobox_tendoituong = Common.StaticVariable.InterfaceObject.SelectCombox(pb);
            Common.Utilities.XuLyComboBox(cbbDoiTuong, combobox_tendoituong, Common.Constants.TenCotTenHienThi, Common.Constants.TenCotKhoaChinh);
        }

        #endregion

        #region Mouse down event
        private void MouseDown_Chung(object sender, MouseEventArgs e)
        {
            try
            {
                Button Img = (Button)sender;
                switch (Img.Name)
                {
                    case Common.Constants.btnThoat:
                        {
                            Thoat();
                            break;
                        }
                    case Common.Constants.btnGhiLai:
                        {
                            if (hanhdong == Common.Constants.Insert)
                            {
                                Them();
                            }
                            break;
                        }
                    case Common.Constants.btnIn:
                        {
                            In();
                            break;
                        }
                }
            }
            catch
            {
            }
           
        }
        #endregion

        #region Control Event
        #region KeyDown
        private void cbbKhoanMuc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtMaDoiTuong.Focus();
            }
        }
        private void txtDoiTuong1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtNguoiNopTien.Focus();
            }
        }
        private void txtNguoiNop_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lbNoTaiKhoan.Focus();
            }
        }
        private void cbbNoTaiKhoan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cbbCoTaiKhoan.Focus();
            }
        }
        private void cbbCoTaiKhoan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtTKNganHang.Focus();
            }
        }

        private void txtTKNganHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtSoTienThanhToan.Focus();
            }
        }

        private void txtSoTienThanhToan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtGhiChu.Focus();
            }
        }
        private void cbbTenTienTe_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtSoTienThanhToan.Focus();
            }
        }
        private void txtGhiChu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cbbKhoanMuc.Focus();
            }
        }
        private void txtDoiTuong2_KeyDown(object sender, KeyEventArgs e)
        {

            txtMaDoiTuong.Text = "";
            if (e.KeyCode == Keys.Enter)
            {
                txtMaDoiTuong.Text = "";
                txtNguoiNopTien.Focus();
            }
            if (e.KeyCode == Keys.F4)
            {
                try
                {
                    if (cbbDoiTuong.Text == "Khách Hàng")
                    {
                        TimKiem.frmKhachHang a = new TimKiem.frmKhachHang("KH");
                        a.ShowDialog();
                        if (TimKiem.frmKhachHang.khachhang != null)
                        {
                            lblMaDoiTuong.Text = TimKiem.frmKhachHang.khachhang.MaKhachHang;
                            txtMaDoiTuong.Text = TimKiem.frmKhachHang.khachhang.MaKhachHang;
                            lblTenDoiTuong.Text = TimKiem.frmKhachHang.khachhang.Ten;
                            lblDiaChiDoiTuong.Text = TimKiem.frmKhachHang.khachhang.DiaChi;
                            txtMaDoiTuong.Text = TimKiem.frmKhachHang.khachhang.MaKhachHang;
                            TimKiem.frmKhachHang.khachhang = null;
                        }
                    }
                    if (cbbDoiTuong.Text == "Nhân Viên")
                    {
                        TimKiem.frmTimKiemNhanVien TimKiemKhachHang = new TimKiem.frmTimKiemNhanVien();
                        TimKiemKhachHang.ShowDialog();
                        if (TimKiem.frmTimKiemNhanVien.nhanvien != null)
                        {
                            lblMaDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.MaNhanVien;
                            txtMaDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.MaNhanVien;
                            lblTenDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.TenNhanVien;
                            lblDiaChiDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.DCThuongTru;
                            txtMaDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.MaNhanVien;
                            TimKiem.frmTimKiemNhanVien.nhanvien = null;
                        }
                    }
                    if (cbbDoiTuong.Text == "Nhà Cung Cấp")
                    {
                        TimKiem.frmTimKiemNCC TimKiemNhaCungCap = new TimKiem.frmTimKiemNCC();
                        TimKiemNhaCungCap.ShowDialog();
                        if (TimKiem.frmTimKiemNCC.NhaCungCap != null)
                        {
                            lblMaDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.MaNhaCungCap;
                            txtMaDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.MaNhaCungCap;
                            lblTenDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.TenNhaCungCap;
                            lblDiaChiDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.DiaChi;
                            txtMaDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.MaNhaCungCap;
                            TimKiem.frmTimKiemNCC.NhaCungCap = null;
                        }
                    }
                }
                catch 
                {
                }
            }
        }

        #endregion

        #region xử lý ID

        #region GetID
        public string getID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "PhieuThu";
                Entities.GetID get = Common.StaticVariable.InterfaceObject.SelectID(id);

                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "PT_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "PT_0001";
            }
            return code;
        }
        #endregion
        #endregion

        #region check validate

        public bool Validate()
        {
            try
            {
                if (txtNguoiNopTien.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbNguoiNhanTien.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtNguoiNopTien.Focus();
                    return false;
                }
                if (cbbKhoanMuc.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbKhoanMuc.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    cbbKhoanMuc.Focus();
                    return false;
                }
                if (cbbDoiTuong.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbDoiTuong.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    cbbDoiTuong.Focus();
                    return false;
                }
                if (cbbCoTaiKhoan.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbCoTaiKhoan.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    cbbCoTaiKhoan.Focus();
                    return false;
                }
                if (lbNoTaiKhoan.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbNoTaiKhoan.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    lbNoTaiKhoan.Focus();
                    return false;
                }
                if (txtSoTienThanhToan.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbSoTienThanhToan.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtSoTienThanhToan.Focus();
                    return false;
                }
                return true;
            }
            catch 
            {
                return true;
                
            }
             
            
        }
        #endregion

        #region XuLyDuLieu
        public void XuLyDuLieu(Entities.PhieuThu phieuthu)
        {
            try
            {
                lbMaPhieuThu.Text = phieuthu.MaPhieuThu;
                txtMaDoiTuong.Text = phieuthu.MaDoiTuong;
                lblMaDoiTuong.Text = phieuthu.MaDoiTuong;
                cbbKhoanMuc.Text = phieuthu.KhoanMuc;
                cbbDoiTuong.Text = phieuthu.DoiTuong;
                lblTenDoiTuong.Text = phieuthu.TenDoiTuong;
                lblDiaChiDoiTuong.Text = phieuthu.DiaChi;
                phieuthu.MaSoThue = "";
                txtNguoiNopTien.Text = phieuthu.NguoiNopTien;
                cbbCoTaiKhoan.Text = phieuthu.CoTaiKhoan;
                lbNoTaiKhoan.Text = phieuthu.NoTaiKhoan;
                txtSoTienThanhToan.Text = Common.Utilities.FormatMoney(phieuthu.TongTienThanhToan);
                txtGhiChu.Text = phieuthu.GhiChu;
            }
            catch 
            {
                
                
            }
         
        }
        #endregion

        #region Xử lý đối tượng
        // Insert data of phiếu chi into database
        public Entities.PhieuThu XuLyObject(string hanhdong)
        {
            try
            {
                if (Validate())
                {
                    Entities.PhieuThu phieuthu = new Entities.PhieuThu();

                    phieuthu.HanhDong = hanhdong;
                    phieuthu.MaPhieuThu = lbMaPhieuThu.Text.ToString();
                    phieuthu.NgayLap = DateTime.Parse(Common.StaticVariable.InterfaceObject.SelectDate().ToString("MM/dd/yyyy"));
                   
                    phieuthu.MaDoiTuong = lblMaDoiTuong.Text.ToString();
                    phieuthu.KhoanMuc = cbbKhoanMuc.Text.ToString();
                    phieuthu.DoiTuong = cbbDoiTuong.Text.ToString();
                    phieuthu.TenDoiTuong = lblTenDoiTuong.Text.ToString();
                    phieuthu.DiaChi = lblDiaChiDoiTuong.Text.ToString();
                    phieuthu.MaSoThue = "123";
                    phieuthu.NguoiNopTien = txtNguoiNopTien.Text.ToString();
                    phieuthu.MaTKNganHang = txtTKNganHang.Text;
                    phieuthu.CoTaiKhoan = cbbCoTaiKhoan.Text.ToString();
                    phieuthu.NoTaiKhoan = lbNoTaiKhoan.Text.ToString();
                    phieuthu.TongTienThanhToan = (txtSoTienThanhToan.Text);
                    phieuthu.MaKho = Entitystatic.ViewFile().MaKho;
                    phieuthu.MaNhanVien = UtilitiesVariable.User.MaNhanVien;
                    phieuthu.GhiChu = txtGhiChu.Text.ToString();
                    phieuthu.Deleted = false;
                    phieuthu.TaiKhoan = UtilitiesVariable.User;
                    return phieuthu;
                }
                return null;
            }
            catch
            {

                return null;
            }

        }
        #endregion

        private void txtTKNganHang_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string tt = String.Format("{0:0}", txtSoTienThanhToan.Text);
                if (float.Parse(tt) >= 0)
                {
                    float test = float.Parse(tt);
                    return;
                }
                txtSoTienThanhToan.Text = "";
            }
            catch
            {
                txtSoTienThanhToan.Text = "";
            }
        }

        private void txtSoTienThanhToan_TextChanged(object sender, EventArgs e)
        {
          
                TextBox temp = (TextBox)sender;
                int vt = temp.SelectionStart;
                string str = new string(temp.Text.ToList<char>().Where(c => char.IsNumber(c)).ToArray<char>());
                temp.Text =str;
                temp.SelectionStart = vt;
       
        }

        private void txtMaDoiTuong_MouseLeave(object sender, EventArgs e)
        {
            if (txtMaDoiTuong.Text == "")
            {
                txtMaDoiTuong.Text = "<F4 - Tra cứu>";
            }
        }

        private void txtMaDoiTuong_MouseMove(object sender, MouseEventArgs e)
        {
            if (txtMaDoiTuong.Text == "<F4 - Tra cứu>")
            {
                txtMaDoiTuong.Text = "";
            }
        }

        private void txtNguoiNopTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                Common.ValidateControl.CheckNumber(e);
            }
            catch
            {
            }
        }
        #endregion

      

     

    }
}
